﻿Public Class SP_Xuat
    Dim xuatDT As NhapXuatDT
    Dim xuatBL As NhapXuatBL
    Dim xctDT As XuatChiTietDT
    Dim xctBL As XuatChiTietBL
    Dim checkclass As New CheckClass

    Private Sub LoadCBox()
        Dim khBL As New KhachHangBL
        Dim spBL As New SanPhamBL
        Dim list_kh As List(Of KhachHangDT)
        Dim list_sp As List(Of SanPhamDT)
        list_kh = khBL.GetData("")
        list_sp = spBL.GetData("")

        With cbKH
            .DataSource = list_kh
            .DisplayMember = "TenKH"
            .ValueMember = "MaKH"
            .SelectedIndex = 0
        End With
        With cbLoai
            .DataSource = list_sp
            .DisplayMember = "TenLoai"
            .ValueMember = "MaLoai"
            .SelectedIndex = 0
        End With
    End Sub

    Private Sub SP_Xuat_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadCBox()
    End Sub

    Private Sub xuat_chitiet(ByVal maxuat As String)
        If IsNumeric(txtSL.Text) = True Then
            'Them chi tiet
            xctBL = New XuatChiTietBL
            Dim stt As Integer = xctBL.GetSTT(maxuat)
            Dim sp As New SanPhamBL
            Dim trigia As Double = sp.getTriGia(cbLoai.SelectedValue)
            Dim thanhtien As Double = Double.Parse(txtSL.Text) * trigia
            If cbKH.Enabled = True Then
                xctDT = New XuatChiTietDT(maxuat, stt, Convert.ToInt32(cbLoai.SelectedValue), txtSL.Text, cbKH.SelectedValue.ToString(), txtGhiChu.Text, thanhtien)
            Else
                xctDT = New XuatChiTietDT(maxuat, stt, Convert.ToInt32(cbLoai.SelectedValue), txtSL.Text, txtGhiChu.Text, thanhtien)
            End If
            xctBL.InXuatCT(xctDT)
            xuatBL = New NhapXuatBL
            Dim tongtien As Double = xuatBL.getTongTien(maxuat)
            tongtien = tongtien + thanhtien
            'Update TongTien cho Xuat
            xuatDT = New NhapXuatDT(maxuat, tongtien)
            xuatBL.InUpXuat(xuatDT, False)
        Else
            txtSL.BackColor = Color.MistyRose
            txtSL.Text = ""
            Return
        End If
    End Sub

    Private Sub xuat_sp()
        If txtSL.Text = "" Or cbLoai.Text = "Chưa chọn loại" Then
            MessageBox.Show("Xuất sản phẩm phải có loại và số lượng sản phẩm")
        Else
            Dim ma As String
            Dim i As Integer
            xuatBL = New NhapXuatBL
            'kiem tra ma xuat cua today co chua ? neu co roi thi xuat vao chi tiet truc tiep
            If checkclass.IsExist(" SanPham_Xuat", " where ngayXuat= '" + Date.Today + "'") = True Then
                ma = xuatBL.GetMa(" SanPham_Xuat", "ngayXuat", Date.Today, "MaXuat")
                'Them vao chitiet
                xuat_chitiet(ma)
            Else
                i = xuatBL.GetID(" SanPham_Xuat_ChiTiet")
                ma = "X" + i.ToString("00000")
                xuatDT = New NhapXuatDT(ma, Date.Today, Main.NVid, 0)
                xuatBL.InUpXuat(xuatDT, True)
                'Them vao chi tiet
                xuat_chitiet(ma)
            End If
        End If
    End Sub

    Public Sub Xuat(ByVal sender As System.Object, ByVal e As System.EventArgs)
        xuat_sp()
        txtSL.Text = ""
        txtGhiChu.Text = ""
    End Sub

    Private Sub cbLoai_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbLoai.SelectedIndexChanged
        If cbLoai.SelectedIndex <> 0 Then
            lbDVT.Visible = True
            Dim dt As New DataTable
            Dim cn As New DatabaseConnect
            dt = cn.GetData("select dvt.donvitinh from SanPham sp,SanPham_DonViTinh dvt where sp.donvitinh = dvt.donvitinhID and sp.MaLoai =" & cbLoai.SelectedValue & "")
            lbDVT.Text = dt.Rows(0)("donvitinh").ToString()
        End If
    End Sub

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        Dim kh As New KH_Nhap()
        kh.flagIsUp = True
        kh.ShowDialog()
        LoadCBox()
        Dim khBL As New KhachHangBL
        Dim i As String = khBL.GetInserted()
        cbKH.SelectedValue = i
    End Sub

    Private Sub txtSL_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSL.TextChanged
        If txtSL.Text <> "" Then
            If IsNumeric(txtSL.Text) = False Then
                txtSL.BackColor = Color.Chocolate
                txtSL.ForeColor = Color.White
            Else
                Dim sp As New SanPhamBL
                Dim trigia As Double = sp.getTriGia(cbLoai.SelectedValue)
                Dim thanhtien As Double = Double.Parse(txtSL.Text) * trigia
                lbThanhtien.Text = thanhtien.ToString()
                txtSL.BackColor = Color.White
                txtSL.ForeColor = Color.Black
            End If
        End If
    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked = True Then
            cbKH.Enabled = False
        Else
            cbKH.Enabled = True
        End If
    End Sub
End Class
